package com.itheima.reggie.service.impl;

/**
 * @Classname EmployeeServiceImpl
 * @Description TODO
 * @Date 2023/3/1 14:21
 * @Created by 11404
 */

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;

import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private HttpSession session;

    // 员工登录
    @Override
    public Employee login(String username, String password) {
        // 1.先根据用户名查询数据库
        Employee employee = employeeMapper.findByUsername(username);
        // 2.判断未查到
        if (employee == null) {
            throw new CustomException("此用户不存在");
        }
        // 3.比较密码
        // 3-1 将用户明文转为密文
        String md5 = SecureUtil.md5(password);
        // 3-2 比较
        if (!StrUtil.equals(md5, employee.getPassword())) {
            throw new CustomException("密码错误");
        }
        // 4.判断员工状态
        if (employee.getStatus() != 1) {
            throw new CustomException("此账号被禁用，请联系管理员");
        }
        // 5.登录成功
        session.setAttribute(Constant.SESSION_EMPLOYEE, employee);

        return employee;

    }


    // 员工列表
    @Override
    public List<Employee> find(String name) {
        return employeeMapper.find(name);
    }

    //新增员工
    @Override
    public void insertSave(Employee employee) {   //接收参数

        //补全信息   所以都用employee存数据
        //ID雪花算法   IdUtil.createSnowflake(？，？) 方法
        long employeeId = IdUtil.createSnowflake(1, 1).nextId();
        employee.setId(employeeId);

        //补全初始密码     SecureUtil.md5() 这个是MD5的方法
        String md5 = SecureUtil.md5(Constant.INIT_PASSWORD);
        employee.setPassword(md5);

        //员工状态
        employee.setStatus(Employee.STATUS_ENABLE);

//        //创建及修改时间     new Date() 方法创建当前时间
//        employee.setUpdateTime(new Date());
//        employee.setCreateTime(new Date());
//
//        //创建及修改人     这里是用session获取整个登录用户的信息，然后用ID查询到登录人名
//        Employee cureentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
//        employee.setCreateUser(cureentEmp.getId());  //这里把登陆人名放到创建人名里
//        employee.setUpdateUser(cureentEmp.getId());  //这里把登陆人名放到修改人名里

        //保存信息   把该方法内补全后的employee对象传给持久层
        employeeMapper.insertEmp(employee);
    }

    //回显员工数据  --  根据ID查询员工数据
    @Override
    public Employee findById(Long id) {

        return employeeMapper.EmpById(id);
    }

    //修改员工信息
    @Override
    public void updateEmployee(Employee employee) {
//
//        //修改信息的时间
//        employee.setUpdateTime(new Date());
//
//        //修改人的姓名
//        Employee cureentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
//        employee.setCreateUser(cureentEmp.getId());

        employeeMapper.udEmployee(employee);

    }

}